﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace AssessmentActions
{
    [Serializable()]
    public class TrueFalseQuestion : Question, ISerializable
    {
        private bool _correctanswer;

        /// <summary>
        /// Constructor to create TrueFalseQuestion with all parameters
        /// </summary>
        /// <param name="id">Question ID</param>
        /// <param name="text">Question Text</param>
        /// <param name="answer">Answer as bool</param>
        public TrueFalseQuestion(int id, string text, bool answer)
        {
            this._id = id;
            this._text = text;
            this._correctanswer = answer;
        }

        /// <summary>
        /// Gets the HTML representation of the question
        /// </summary>
        /// <returns>String version of question</returns>
        public override string getQuestionHtml()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<div class=\"question_true_false\">");
            sb.Append("<span class=\"text\">" + _text + "</span>");
            sb.Append("</div>");

            return sb.ToString();
        }

        /// <summary>
        /// Gets the HTML representation of the question with answer
        /// </summary>
        /// <returns>String version of question</returns>
        public override string getQuestionWithAnswerHtml()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<div class=\"question_true_false\">");
            sb.Append("<span class=\"text\">" + _text + "</span>");
            sb.Append("<span class=\"answer\">");
            if (_correctanswer)
            {
                sb.Append("True");
            }
            else
            {
                sb.Append("False");
            }
            sb.Append("</span>");
            sb.Append("</div>");

            return sb.ToString();
        }

        public override string getQuestionHtmlWithSelector()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<div class=\"question_true_false\">");
            sb.Append("<span id=\"questionId\">tf_" + _id + "</span> ");
            sb.Append("<span class=\"text\">" + _text + "</span>");
            sb.Append("<ul class=\"choiceList\">");
            sb.Append("<li class=\"choice\">");
            sb.Append("<input type=\"radio\" name=\"tf_answers_" + this._id + "\" value=\"T\" class=\"trueFalseRadioButton\" />True");
            sb.Append("</li>");
            sb.Append("<li class=\"choice\">");
            sb.Append("<input type=\"radio\" name=\"tf_answers_" + this._id + "\" value=\"F\" class=\"trueFalseRadioButton\" />False");
            sb.Append("</li>");
            sb.Append("</ul>");
            sb.Append("</div>");

            return sb.ToString();
        }

        /// <summary>
        /// Gets the XML representation of the question
        /// </summary>
        /// <returns>String version of question</returns>
        public override string getQuestionXml()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("\n\t\t<TrueFalseQuestion>");
            sb.Append("\n\t\t\t<text>" + _text + "</text>");
            sb.Append("\n\t\t</TrueFalseQuestion>");

            return sb.ToString();
        }

        /// <summary>
        /// Gets the XML representation of the question with answer
        /// </summary>
        /// <returns>String version of question</returns>
        public override string getQuestionWithAnswerXml()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("\n\t\t<TrueFalseQuestion>");
            sb.Append("\n\t\t\t<text>" + _text + "</text>");
            sb.Append("\n\t\t\t<answer>");
            if (_correctanswer)
            {
                sb.Append("True");
            }
            else
            {
                sb.Append("False");
            }
            sb.Append("</answer>");
            sb.Append("\n\t\t</TrueFalseQuestion>");

            return sb.ToString();
        }

        public bool Correctanswer
        {
            get { return _correctanswer; }
            set { _correctanswer = value; }
        }

        public override string getCorrectAnswer()
        {
            if (_correctanswer)
                return "T";
            else
                return "F";
        }

        /// <summary>
        /// Deserialization constructor
        /// </summary>
        /// <param name="info"></param>
        /// <param name="ctxt"></param>
        public TrueFalseQuestion(SerializationInfo info, StreamingContext ctxt)
        {
            _id = (int)info.GetValue("_id", typeof(int));
            _text = (String)info.GetValue("_text", typeof(String));
            _correctanswer = (Boolean)info.GetValue("_correctanswer", typeof(Boolean));
        }

        /// <summary>
        /// Serialization function
        /// </summary>
        /// <param name="info"></param>
        /// <param name="ctxt"></param>
        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("_id", _id);
            info.AddValue("_text", _text);
            info.AddValue("_correctanswer", _correctanswer);
        }


        

    }

}
